package restsrv

import (
	"bytes"
	"crypto/tls"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"strings"
	"time"
)

//= "https://10.154.2.79:443"
var client *http.Client

func PostJson(url string, body []byte) ([]byte, error) {

	if strings.HasPrefix(url, "https") {
		return send("POST", url, body)
	} else {
		return sendWithOutSec("POST", url, body)
	}
}

func GetApp() ([]byte, error) {

	return send("GET", TestHost+"/test/app", []byte(""))

}

func Get() ([]byte, error) {

	return send("GET", TestHost, []byte(""))

}

func send(method string, url string, body []byte) ([]byte, error) {

	reqBody := bytes.NewBuffer(body)

	if client == nil {

		client = &http.Client{
			Transport: &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
				Dial: func(netw, addr string) (net.Conn, error) {
					conn, err := net.DialTimeout(netw, addr, time.Second*1)
					if err != nil {
						return nil, err
					}
					conn.SetDeadline(time.Now().Add(time.Second * 1))
					return conn, nil
				},
				DisableKeepAlives:     false,
				MaxConnsPerHost:       200,
				MaxIdleConns:          1000,
				MaxIdleConnsPerHost:   500,
				ResponseHeaderTimeout: time.Second * 1,
			},
		}
	}

	req, err := http.NewRequest(method, url, reqBody)
	if nil != err {
		fmt.Print("new request failed.", err)
		return nil, err
	}

	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json;charset=utf-8")
	req.Header.Set("Connection", "close")

	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf("http client failed:%v\n", err)
		return nil, errors.New("http request failed!")
	}

	defer resp.Body.Close()

	if 200 != resp.StatusCode {
		fmt.Print("post url ", url, "failed.status code:", resp.StatusCode, "\n")
		return nil, errors.New("return status code not 200")
	}

	resbody, err := ioutil.ReadAll(resp.Body)
	return resbody, err
}

func sendWithOutSec(method string, url string, body []byte) ([]byte, error) {

	reqBody := bytes.NewBuffer(body)

	if client == nil {

		client = &http.Client{
			Transport: &http.Transport{
				Dial: func(netw, addr string) (net.Conn, error) {
					conn, err := net.DialTimeout(netw, addr, time.Second*1)
					if err != nil {
						return nil, err
					}
					conn.SetDeadline(time.Now().Add(time.Second * 1))
					return conn, nil
				},
				ResponseHeaderTimeout: time.Second * 1,
				DisableKeepAlives:     false,
				MaxConnsPerHost:       200,
				MaxIdleConns:          1000,
				MaxIdleConnsPerHost:   500,
			},
		}
	}
	req, err := http.NewRequest(method, url, reqBody)
	if nil != err {
		fmt.Print("new request failed.", err)
		return nil, err
	}

	req.Header.Set("Accept", "application/json")
	req.Header.Set("Content-Type", "application/json;charset=utf-8")
	req.Header.Set("Connection", "close")

	resp, err := client.Do(req)
	if err != nil {
		fmt.Print("http client failed:", err)
		return nil, errors.New("http request failed!")
	}

	defer resp.Body.Close()

	if 200 != resp.StatusCode {
		fmt.Print("post url ", url, "failed.status code:", resp.StatusCode)
		return nil, errors.New("return status code not 200")
	}

	resbody, err := ioutil.ReadAll(resp.Body)
	return resbody, err
}

type callBackInfo struct {
	AppId         string `json:"appId"`
	Caller        string `json:"caller"`
	Caller2       string `json:"caller2"`
	Called1       string `json:"called1"`
	Called2       string `json:"called2"`
	Data          string `json:"data"`
	Timeout       int    `json:"timeout"`
	CalleeDisPlay string `json:"calledDisPlay"`
}

type outCallInfo struct {
	AppId      string `json:"appId"`
	Caller     string `json:"caller"`
	Called     string `json:"called"`
	Data       string `json:"data"`
	Timeout    int    `json:"timeout"`
	AiFlag     int    `json:"enableAi"`
	Display    string `json:"calledDisPlay"`
	RecordType string `json:"recordType"`
}

type ttsObj struct {
	EngineType  string                 `json:"engineType"`
	ConvertType string                 `json:"convertType"`
	Args        map[string]interface{} `json:"args"`
}

type playInfo struct {
	AppId     string `json:"appId"`
	CallId    string `json:"callId"`
	GetKey    int    `json:"getkey"`
	Flag      int    `json:"playFlag"`
	Voice     string `json:"voiceStr"`
	PlayTime  int    `json:"playTime"`
	MaxKey    int    `json:"maxRevCnt"`
	Key2End   string `json:"key2End"`
	SpaceTime int    `json:"spaceTime"`
	TotalTime int    `json:"totalTime"`
	Data      string `json:"data"`
	KeyStop   int    `json:"keyStop"`
	TtsEngine ttsObj `json:"ttsEngine"`
}

type rspon struct {
	Code   string `json:"retCode"`
	Appid  string `json:"appid"`
	Callid string `json:"callid"`
}

type callstat struct {
	callid     string
	appid      string
	caller     string
	called     string
	status     string
	startTime  time.Time
	ringTime   time.Time
	answerTime time.Time
	hunTime    time.Time
}

type PlayInCallInfo struct {
	AppId       string `json:"appId"`
	CallId      string `json:"callId"`
	PlayChannel string `json:"playChannel"` //caller1，caller2，both
	Flag        int    `json:"playFlag"`
	Voice       string `json:"voiceStr"`
	PlayTime    int    `json:"playTime"`
	TtsEngine   ttsObj `json:"ttsEngine"`
	GetKey      int    `json:"getKey"`
}

func TestOutCallBack(appid string, number1, number2, display string, caller string) string {
	url := TestHost + "/ipcc/call/callBack"

	param := callBackInfo{
		AppId:  appid,
		Caller: caller,
		//"01067440695",
		Called1:       number1,
		Data:          "testCallOut",
		Called2:       number2,
		CalleeDisPlay: display,
	}

	fmt.Println("call back number[%v] start", number1)

	jsonReq, _ := json.Marshal(param)

	resbody, err := PostJson(url, jsonReq)
	if nil != err {
		fmt.Print(err)
		return ""
	}
	fmt.Print(string(resbody))

	var result rspon
	json.Unmarshal(resbody, &result)

	var stat callstat
	stat.appid = appid
	stat.callid = result.Callid
	stat.startTime = time.Now()

	Callist.Store(result.Callid, &stat)

	return result.Callid
}

func TestOutCall(appid string, number string, caller string) string {
	url := TestHost + "/ipcc/call/outCall"

	param := outCallInfo{
		AppId:  appid,
		Caller: caller,
		//"01067440695",
		Called:     number,
		Data:       "testCallOut",
		Timeout:    50,
		RecordType: recordType,
	}

	fmt.Println("call number start", number)

	jsonReq, _ := json.Marshal(param)

	resbody, err := PostJson(url, jsonReq)
	if nil != err {
		fmt.Print(err)
		return ""
	}
	fmt.Printf("outcall create result:%v", string(resbody))

	var result rspon
	json.Unmarshal(resbody, &result)

	var stat callstat
	stat.appid = appid
	stat.callid = result.Callid
	stat.startTime = time.Now()

	Callist.Store(result.Callid, &stat)

	return result.Callid
}

func TestOutCallWithAi(appid string, number string, caller string) string {
	url := TestHost + "/ipcc/call/outCall"

	param := outCallInfo{
		AppId:  appid,
		Caller: caller,
		//"01067440695",
		Called:     number,
		Data:       "testCallOut",
		Timeout:    50,
		AiFlag:     1,
		RecordType: recordType,
	}

	aiflag = true

	fmt.Printf("call number start:%s\n", number)

	jsonReq, _ := json.Marshal(param)

	resbody, err := PostJson(url, jsonReq)
	if nil != err {
		fmt.Print(err)
		return ""
	}
	fmt.Printf("outcall create result:%v", string(resbody))

	var result rspon
	json.Unmarshal(resbody, &result)

	var stat callstat
	stat.appid = appid
	stat.callid = result.Callid
	stat.startTime = time.Now()

	Callist.Store(result.Callid, &stat)

	return result.Callid
}

func testPlayTTSInCall(appid, callid, number, ttsText string, getKey int) {

	play := PlayInCallInfo{
		AppId:       appid,
		CallId:      callid,
		PlayChannel: number,
		Flag:        0,
		PlayTime:    1,
		Voice:       ttsText,
		GetKey:      getKey,
	}

	jsonReq, _ := json.Marshal(play)
	resbody, err := PostJson(TestHost+"/ipcc/call/playIncall", jsonReq)

	if nil != err {
		fmt.Print(err)
		return
	}
	fmt.Print(string(resbody))

}

func testPlayTTS(appid string, callid, ttsText string) {

	args := make(map[string]interface{})
	args["speed"] = ttsSpeed
	args["volume"] = 0.25

	play := playInfo{
		AppId:     appid,
		CallId:    callid,
		GetKey:    0, //1为获取DTMF
		Flag:      0, //0为播放TTS
		Voice:     ttsText,
		PlayTime:  3,   //播放次数
		MaxKey:    0,   //最大获取按键数
		Key2End:   "#", //按键停止
		SpaceTime: 0,   //按键间隔
		TotalTime: 30,  //总按键时长
		Data:      "testTTS",
		KeyStop:   0,
		TtsEngine: ttsObj{"ttsEngineQcloud", "", args},
	}

	jsonReq, _ := json.Marshal(play)
	resbody, err := PostJson(TestHost+"/ipcc/call/play", jsonReq)

	if nil != err {
		fmt.Print(err)
		return
	}
	fmt.Print("playtts:", string(resbody))
}

func testPlayFile(appid string, callid string, file string) {

	play := playInfo{
		AppId:     appid,
		CallId:    callid,
		GetKey:    0, //1为获取DTMF
		Flag:      1, //0为播放TTS
		Voice:     file,
		PlayTime:  3,   //播放次数
		MaxKey:    0,   //最大获取按键数
		Key2End:   "#", //按键停止
		SpaceTime: 0,   //按键间隔
		TotalTime: 30,  //总按键时长
		Data:      "testTTS",
		KeyStop:   1,
	}

	jsonReq, _ := json.Marshal(play)
	resbody, err := PostJson(TestHost+"/ipcc/call/play", jsonReq)

	if nil != err {
		fmt.Print(err)
		return
	}
	fmt.Print("play:", string(resbody))
}

func testGetDTMFWithTTSex(appid string, callid string) {

	var play playInfo

	play.AppId = appid
	play.CallId = callid
	play.GetKey = 1
	play.Flag = 0
	play.Voice = "我[p500]测试[p500]你"
	play.Key2End = "#"
	play.MaxKey = 6
	play.PlayTime = 1
	play.SpaceTime = 10
	play.TotalTime = 20

	jsonReq, _ := json.Marshal(play)
	_, err := PostJson(TestHost+"/ipcc/call/play", jsonReq)

	if nil != err {
		fmt.Print(err)
		return
	}

}

func testGetDTMFWithTTS(appid string, callid string) {

	var play playInfo
	play.AppId = appid
	play.CallId = callid
	play.GetKey = 1
	play.Flag = 0
	play.Voice = "请按键 滴"
	play.Data = "2"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60
	play.KeyStop = 1

	jsonReq, _ := json.Marshal(play)
	resbody, err := PostJson(TestHost+"/ipcc/call/play", jsonReq)

	if nil != err {
		fmt.Print(err)
		return
	}
	fmt.Print("getdtmf", string(resbody))
}

func testHungup(appid string, callid string) {

	fmt.Printf("hungup the call :%s\n", callid)

	hangup := hungupInfo{appid, callid}

	jsonReq, _ := json.Marshal(hangup)
	resbody, err := PostJson(TestHost+"/ipcc/call/disConnect", jsonReq)
	if nil != err {

		fmt.Print(err)
		return
	}

	fmt.Printf("hungup call result :%s\n", string(resbody))
}

type multyInfo struct {
	Appid    string     `json:"appid"`
	Callid   string     `json:"callid"`
	PlayTime int        `json:"playTime"`
	Orders   []playInfo `json:"orders"`
}

func testMultApi(appid string, callid string) {

	var call multyInfo
	call.Appid = appid
	call.Callid = callid
	call.PlayTime = 1

	var play playInfo

	play.AppId = appid
	play.CallId = callid
	play.Flag = 1
	play.Voice = "WEBANK/374.vox"
	play.Data = "1"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 1
	play.Voice = "WEBANK/57.vox"
	play.Data = "2"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 1
	play.Voice = "WEBANK/75.vox"
	play.Data = "3"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	ttsArgs := make(map[string]interface{})
	ttsArgs["speed"] = ttsSpeed
	ttsArgs["volume"] = 0.25

	play.AppId = appid
	play.CallId = callid
	play.Flag = 0
	play.Voice = "邓姝梅"
	play.Data = "4"
	play.Key2End = "#"
	play.PlayTime = 1
	play.TtsEngine = ttsObj{"ttsEngineQcloud", "0", ttsArgs}
	count++

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 1
	play.Voice = "WEBANK/5.vox"
	play.Data = "5"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "WEBANK/375.vox"
	play.Data = "6"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "WEBANK/378.vox"
	play.Data = "7"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 0
	play.Voice = "3461.00元"
	play.Data = "8"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "WEBANK/313.vox"
	play.Data = "9"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.Flag = 0
	play.Voice = time.Now().Format("2006-01-02")
	play.Data = "10"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/314.vox"
	play.Data = "11"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/315.vox"
	play.Data = "12"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/316.vox"
	play.Data = "13"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/377.vox"
	play.Data = "14"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/73.vox"
	play.Data = "15"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/377.vox"
	play.Data = "16"
	play.Key2End = "#"
	play.MaxKey = 1
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/74.vox"
	play.Data = "17"
	play.Key2End = "#"
	play.MaxKey = 1
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/69.vox"
	play.Data = "18"
	play.Key2End = "#"
	play.MaxKey = 1
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	//打包发送
	jsonReq, _ := json.Marshal(call)
	resbody, err := PostJson(TestHost+"/ipcc/call/multOrders", jsonReq)
	if nil != err {
		fmt.Print(err)
		return
	}

	if debug {
		fmt.Print("playMulty:", string(resbody))
	}

}

func testMultApiNoTTS(appid string, callid string) {

	var call multyInfo
	call.Appid = appid
	call.Callid = callid
	call.PlayTime = 1

	var play playInfo

	play.AppId = appid
	play.CallId = callid
	play.Flag = 1
	play.Voice = "WEBANK/374.vox"
	play.Data = "1"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 1
	play.Voice = "WEBANK/57.vox"
	play.Data = "2"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 1
	play.Voice = "WEBANK/75.vox"
	play.Data = "3"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 1
	play.Voice = "WEBANK/5.vox"
	play.Data = "5"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "WEBANK/375.vox"
	play.Data = "6"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "WEBANK/378.vox"
	play.Data = "7"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "WEBANK/313.vox"
	play.Data = "9"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/314.vox"
	play.Data = "11"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/315.vox"
	play.Data = "12"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/316.vox"
	play.Data = "13"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/377.vox"
	play.Data = "14"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/73.vox"
	play.Data = "15"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/377.vox"
	play.Data = "16"
	play.Key2End = "#"
	play.MaxKey = 1
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/74.vox"
	play.Data = "17"
	play.Key2End = "#"
	play.MaxKey = 1
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/69.vox"
	play.Data = "18"
	play.Key2End = "#"
	play.MaxKey = 1
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	//打包发送
	jsonReq, _ := json.Marshal(call)
	resbody, err := PostJson(TestHost+"/ipcc/call/multOrders", jsonReq)
	if nil != err {
		fmt.Print(err)
		return
	}

	if debug {
		fmt.Print("playMulty:", string(resbody))
	}

}

func testMultApiTtsRtm(appid string, callid string) {

	var call multyInfo
	call.Appid = appid
	call.Callid = callid
	call.PlayTime = 1

	var play playInfo

	play.AppId = appid
	play.CallId = callid
	play.Flag = 1
	play.Voice = "WEBANK/374.vox"
	play.Data = "1"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 1
	play.Voice = "WEBANK/57.vox"
	play.Data = "2"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 1
	play.Voice = "WEBANK/75.vox"
	play.Data = "3"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	ttsArgs := make(map[string]interface{})
	ttsArgs["speed"] = ttsSpeed
	ttsArgs["volume"] = 0.25

	play.AppId = appid
	play.CallId = callid
	play.Flag = 0
	play.Voice = "邓姝梅"
	play.Data = "4"
	play.Key2End = "#"
	play.PlayTime = 1
	play.TtsEngine = ttsObj{"ttsEngineQcloud", "1", ttsArgs}
	count++

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 1
	play.Voice = "WEBANK/5.vox"
	play.Data = "5"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "WEBANK/375.vox"
	play.Data = "6"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "WEBANK/378.vox"
	play.Data = "7"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 0
	play.Voice = "3461.00元"
	play.Data = "8"
	play.Key2End = "#"
	play.PlayTime = 1

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "WEBANK/313.vox"
	play.Data = "9"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.Flag = 0
	play.Voice = time.Now().Format("2006-01-02")
	play.Data = "10"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/314.vox"
	play.Data = "11"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/315.vox"
	play.Data = "12"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/316.vox"
	play.Data = "13"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/377.vox"
	play.Data = "14"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/73.vox"
	play.Data = "15"
	play.Key2End = "#"
	play.MaxKey = 10
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/377.vox"
	play.Data = "16"
	play.Key2End = "#"
	play.MaxKey = 1
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/74.vox"
	play.Data = "17"
	play.Key2End = "#"
	play.MaxKey = 1
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	play.CallId = callid
	play.GetKey = 0
	play.KeyStop = 1
	play.Flag = 1
	play.Voice = "WEBANK/69.vox"
	play.Data = "18"
	play.Key2End = "#"
	play.MaxKey = 1
	play.PlayTime = 1
	play.SpaceTime = 15
	play.TotalTime = 60

	call.Orders = append(call.Orders, play)

	//打包发送
	jsonReq, _ := json.Marshal(call)
	resbody, err := PostJson(TestHost+"/ipcc/call/multOrders", jsonReq)
	if nil != err {
		fmt.Print(err)
		return
	}
	fmt.Print(string(resbody))
}

func testMultApiNewVoice(appid string, callid string) {

	var call multyInfo
	call.Appid = appid
	call.Callid = callid
	call.PlayTime = 1

	var play playInfo
	//节点1
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "1002_f3b73de62b6a97601e67e67df5674a33_27_6.wav"
	play.TtsEngine.EngineType = "ttsEngineQcloud"
	play.TtsEngine.Args = make(map[string]interface{})
	play.TtsEngine.Args["VoiceType"] = 1002

	call.Orders = append(call.Orders, play)

	//节点2
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "1002_de4f6913ed4096107830ac29dac964e7_33_6.wav"
	play.TtsEngine.EngineType = "ttsEngineQcloud"
	play.TtsEngine.Args = make(map[string]interface{})
	play.TtsEngine.Args["VoiceType"] = 1002

	call.Orders = append(call.Orders, play)

	//节点3
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "1002_f99b380ce523b4569e4f257a1c4b5f46_51_6.wav"
	play.TtsEngine.EngineType = "ttsEngineQcloud"
	play.TtsEngine.Args = make(map[string]interface{})
	play.TtsEngine.Args["VoiceType"] = 1002

	call.Orders = append(call.Orders, play)

	//节点4
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "1002_99391bb2473192e2e2fbfd651b08d3e9_42_6.wav"
	play.TtsEngine.EngineType = "ttsEngineQcloud"
	play.TtsEngine.Args = make(map[string]interface{})
	play.TtsEngine.Args["VoiceType"] = 1002

	call.Orders = append(call.Orders, play)

	//节点5
	play.CallId = callid
	play.GetKey = 0
	play.Flag = 1
	play.Voice = "1002_13ea47a2f5f7f1e8d36c51d083d23b26_15_6.wav"
	play.TtsEngine.EngineType = "ttsEngineQcloud"
	play.TtsEngine.Args = make(map[string]interface{})
	play.TtsEngine.Args["VoiceType"] = 1002

	call.Orders = append(call.Orders, play)

	//节点6
	play.CallId = callid
	play.GetKey = 1
	play.Flag = 1
	play.TotalTime = 6
	play.MaxKey = 1
	play.Voice = "1002_e41952a9c17d5e16e3e47b7fd23b295c_14_6.wav"
	play.TtsEngine.EngineType = "ttsEngineQcloud"
	play.TtsEngine.Args = make(map[string]interface{})
	play.TtsEngine.Args["VoiceType"] = 1002

	call.Orders = append(call.Orders, play)

	//打包发送
	jsonReq, _ := json.Marshal(call)
	resbody, err := PostJson(TestHost+"/ipcc/call/multOrders", jsonReq)
	if nil != err {
		fmt.Print(err)
		return
	}
	fmt.Print(string(resbody))

}

func testMultApiAllTTS(appid string, callid string) {

	var call multyInfo
	call.Appid = appid
	call.Callid = callid
	call.PlayTime = 1

	ttsArgs := make(map[string]interface{})
	ttsArgs["speed"] = ttsSpeed
	ttsArgs["volume"] = 0.25

	var play playInfo

	play.AppId = appid
	play.CallId = callid
	play.Flag = 0
	play.Voice = "这是一段全tts测试"
	play.Data = "4"
	play.Key2End = "#"
	play.PlayTime = 1
	play.TtsEngine = ttsObj{"ttsEngineQcloud", "1", ttsArgs}

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 0
	play.Voice = "测试节点2，时间测试：2020-09-27 15:04:47"
	play.Data = "4"
	play.Key2End = "#"
	play.PlayTime = 1
	play.TtsEngine = ttsObj{"ttsEngineQcloud", "1", ttsArgs}

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 0
	play.Voice = "测试节点3，数字测试：118.762元"
	play.Data = "4"
	play.Key2End = "#"
	play.PlayTime = 1
	play.TtsEngine = ttsObj{"ttsEngineQcloud", "1", ttsArgs}

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 0
	play.Voice = "测试节点4，金额测试 130元5角"
	play.Data = "4"
	play.Key2End = "#"
	play.PlayTime = 1
	play.TtsEngine = ttsObj{"ttsEngineQcloud", "1", ttsArgs}

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 0
	play.Voice = "测试节点5，大金额测试 100万3101元5角"
	play.Data = "4"
	play.Key2End = "#"
	play.PlayTime = 1
	play.TtsEngine = ttsObj{"ttsEngineQcloud", "1", ttsArgs}

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 0
	play.Voice = "测试节点6，长字符串测试： 春眠不觉晓，处处蚊子咬，夜来风雨声，花落知多少"
	play.Data = "4"
	play.Key2End = "#"
	play.PlayTime = 1
	play.TtsEngine = ttsObj{"ttsEngineQcloud", "1", ttsArgs}

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 0
	play.Voice = "测试节点7，长字符串测试： 春眠不觉晓，处处蚊子咬，夜来风雨声，花落知多少"
	play.Data = "4"
	play.Key2End = "#"
	play.PlayTime = 1
	play.TtsEngine = ttsObj{"ttsEngineQcloud", "1", ttsArgs}

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 0
	play.Voice = "测试节点8，长字符串测试： 春眠不觉晓，处处蚊子咬，夜来风雨声，花落知多少"
	play.Data = "4"
	play.Key2End = "#"
	play.PlayTime = 1
	play.TtsEngine = ttsObj{"ttsEngineQcloud", "1", ttsArgs}

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 0
	play.Voice = "测试节点9，长字符串测试： 春眠不觉晓，处处蚊子咬，夜来风雨声，花落知多少"
	play.Data = "4"
	play.Key2End = "#"
	play.PlayTime = 1
	play.TtsEngine = ttsObj{"ttsEngineQcloud", "1", ttsArgs}

	call.Orders = append(call.Orders, play)

	play.AppId = appid
	play.CallId = callid
	play.Flag = 0
	play.Voice = "测试节点10，长字符串测试： 春眠不觉晓，处处蚊子咬，夜来风雨声，花落知多少"
	play.Data = "4"
	play.Key2End = "#"
	play.PlayTime = 1
	play.TtsEngine = ttsObj{"ttsEngineQcloud", "1", ttsArgs}

	call.Orders = append(call.Orders, play)

	//打包发送
	jsonReq, _ := json.Marshal(call)
	resbody, err := PostJson(TestHost+"/ipcc/call/multOrders", jsonReq)
	if nil != err {
		fmt.Print(err)
		return
	}

	if debug {
		fmt.Print("playMulty:", string(resbody))
	}

}

type replyInfo struct {
	AppId   string `json:"appId"`
	CallId  string `json:"callId"`
	AnsCode int    `json:"ansCode"`
}

func testAnswerCall(appid string, callid string) {
	reply := replyInfo{
		appid,
		callid,
		0,
	}

	jsonReq, _ := json.Marshal(reply)
	resbody, err := PostJson(TestHost+"/ipcc/call/reply", jsonReq)
	if nil != err {
		fmt.Print(err)
		return
	}

	fmt.Print("接听", string(jsonReq), "recv:", string(resbody))
}

type messageInfo struct {
	AppId   string `json:"appId"`
	CallId  string `json:"callId"`
	Flag    int    `json:"playFlag"`
	Voice   string `json:"voiceStr"`
	Key2End string `json:"key2Stop"`
	Data    string `json:"data"`
}

func testLeavMessage(appid string, callid string) {
	call := messageInfo{
		AppId:   appid,
		CallId:  callid,
		Flag:    0,
		Voice:   "听到低声后请留言 滴",
		Key2End: "*",
		Data:    "leavemsg",
	}

	jsonReq, _ := json.Marshal(call)
	resbody, err := PostJson(TestHost+"/ipcc/call/message", jsonReq)
	if nil != err {
		fmt.Print(err)
		return
	}

	fmt.Print("留言：", string(jsonReq), "recv:", string(resbody))

}

type transferInfo struct {
	AppId  string `json:"appId"`
	CallId string `json:"callId"`
	Called string `json:"called"`
	Voice  string `json:"fileName"`
	Data   string `json:"data"`
}

func testTransfer(appid string, callid string) {

	trans := transferInfo{
		AppId:  appid,
		CallId: callid,
		Called: "18898739887",
		Voice:  "test.wav",
		Data:   "testcallwife2",
	}

	jsonReq, _ := json.Marshal(trans)
	fmt.Printf("ready to send:", time.Now().String())
	resbody, err := PostJson(TestHost+"/ipcc/call/transfer", jsonReq)
	if nil != err {
		fmt.Print(err)
		return
	}

	fmt.Print("转接：", time.Now().String(), string(jsonReq), "recv:", string(resbody))

}

type callNotifyInfo struct {
	AppId      string `json:"appId"`
	Caller     string `json:"caller"`
	Callee     string `json:"called"`
	Voice      string `json:"voice"`
	Flag       int    `json:"flag"`
	GetKey     int    `json:"getKey"`
	PlayTime   int    `json:"playTime"`
	Transfer   string `json:"transfer"`
	AnswerTime int    `json:"answerTime"`
}

func TestCallNotify(appid, callee, caller, voice string) {

	testVoice := "呼叫通知测试"
	if len(voice) > 0 {
		testVoice = voice
	}
	notify := callNotifyInfo{
		AppId:    appid,
		Caller:   caller,
		Callee:   callee,
		Voice:    testVoice,
		Flag:     0,
		GetKey:   1,
		PlayTime: 1,
	}

	jsonReq, _ := json.Marshal(notify)
	resbody, err := PostJson(TestHost+"/ipcc/call/callNotify", jsonReq)
	if nil != err {
		fmt.Print(err)
		return
	}

	fmt.Print("文本长度：", len(notify.Voice), "呼叫通知：", string(jsonReq), "recv:", string(resbody))

}

func TestCallNotifyTransfer(appid, callee, caller, transfer string, answer int) {

	testVoice := "test.wav"

	notify := callNotifyInfo{
		AppId:      appid,
		Caller:     caller,
		Callee:     callee,
		Voice:      testVoice,
		Flag:       1,
		GetKey:     0,
		PlayTime:   1,
		Transfer:   transfer,
		AnswerTime: answer,
	}

	jsonReq, _ := json.Marshal(notify)
	resbody, err := PostJson(TestHost+"/ipcc/call/callNotify", jsonReq)
	if nil != err {
		fmt.Print(err)
		return
	}

	fmt.Print("呼叫通知转接：", string(jsonReq), "\nrecv:", string(resbody), "\n")

}

func TestThreeWay(appid, number, callid string) {

	type ThreeWayInfo struct {
		AppId  string `json:"appId"`
		CallId string `json:"callId"`
		Number string `json:"number"`
		Data   string `json:"data"`
	}

	notify := ThreeWayInfo{
		AppId:  appid,
		CallId: callid,
		Number: number,
		Data:   "test3way",
	}

	jsonReq, _ := json.Marshal(notify)
	resbody, err := PostJson(TestHost+"/ipcc/call/listenTheCall", jsonReq)
	if nil != err {
		fmt.Print(err)
		return
	}

	fmt.Print("三方通话：", string(jsonReq), "\nrecv:", string(resbody), "\n")

}

func TestReplaceRobot(appid, callid string) {

	type ThreeWayInfo struct {
		AppId  string `json:"appId"`
		CallId string `json:"callId"`
		Data   string `json:"data"`
	}

	notify := ThreeWayInfo{
		AppId:  appid,
		CallId: callid,
		Data:   "test3way",
	}

	jsonReq, _ := json.Marshal(notify)
	resbody, err := PostJson(TestHost+"/ipcc/call/replaceTheCall", jsonReq)
	if nil != err {
		fmt.Print(err)
		return
	}

	fmt.Print("三方通话：", string(jsonReq), "\nrecv:", string(resbody), "\n")

}

func TestOutCallBackSip(appid string, number1, number2 string, caller, caller2 string) string {
	url := TestHost + "/ipcc/call/callBackCustormer"

	testAppId = appid
	param := callBackInfo{
		AppId:   appid,
		Caller:  caller,
		Caller2: caller2,
		Called1: number1,
		Data:    "testCallOut",
		Called2: number2,
	}

	fmt.Println("call back number[%v] start", number1)

	jsonReq, _ := json.Marshal(param)

	resbody, err := PostJson(url, jsonReq)
	if nil != err {
		fmt.Print(err)
		return ""
	}
	fmt.Print(string(resbody))

	var result rspon
	json.Unmarshal(resbody, &result)

	var stat callstat
	stat.appid = appid
	stat.callid = result.Callid
	stat.startTime = time.Now()

	Callist.Store(result.Callid, &stat)
	return result.Callid
}

func TestStopPlay(AppId, callId string) {

	type stopPlay struct {
		AppId  string `json:"appId"`
		CallId string `json:"callId"`
		Data   string `json:"data"`
	}

	cmd := stopPlay{
		AppId:  AppId,
		CallId: callId,
	}

	url := TestHost + "/ipcc/call/stoPlay"
	fmt.Println("stop play :%v", callId)

	jsonReq, _ := json.Marshal(cmd)

	_, err := PostJson(url, jsonReq)
	if nil != err {
		fmt.Printf("stop play err:%v", err.Error())
	}

}
